//
//  UINavigationBar+FixSpace.m
//  EPTool
//
//  Created by shiqing guo on 2018/5/9.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "UINavigationBar+FixSpace.h"
#import "NSObject+Swizzle.h"
#import "UIView+DDAddition.h"
#import <objc/runtime.h>

#define IOS11            ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 11.0)

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0

#ifndef ep_defaultFixSpace
#define ep_defaultFixSpace 16
#endif



static BOOL ep_disableFixSpace = NO;
static CGFloat kUINavbarButtonWidth = 80;
static CGFloat kUINavbarButtonHeight = 44;


@implementation UIImagePickerController (FixSpace)
+(void)load {
    if (IOS11) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self swizzleMethod:@selector(viewWillAppear:)
                         withMethod:@selector(ep_viewWillAppear:) error:nil];
            
            [self swizzleMethod:@selector(viewWillDisappear:)
                         withMethod:@selector(ep_viewWillDisappear:) error:nil];
        });
    }
}


-(void)ep_viewWillAppear:(BOOL)animated {
    ep_disableFixSpace = YES;
    [self ep_viewWillAppear:animated];
}

-(void)ep_viewWillDisappear:(BOOL)animated{
    ep_disableFixSpace = NO;
    [self ep_viewWillDisappear:animated];
}

@end

@implementation UINavigationBar (FixSpace)

+(void)load {
    if (IOS11) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self swizzleMethod:@selector(layoutSubviews)
                         withMethod:@selector(ep_layoutSubviews) error:nil];
            [self swizzleMethod:@selector(hitTest:withEvent:)
                         withMethod:@selector(ep_hitTest:withEvent:) error:nil];
            
        });
    }
    
}

-(void)ep_layoutSubviews{
    [self ep_layoutSubviews];
    
    if (IOS11 && !ep_disableFixSpace) {//需要调节
        self.layoutMargins = UIEdgeInsetsZero;
        CGFloat space = ep_defaultFixSpace;
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass(subview.class) containsString:@"ContentView"]) {
                subview.layoutMargins = UIEdgeInsetsMake(0, ep_defaultFixSpace, 0, ep_defaultFixSpace);//可修正iOS11之后的偏移
                break;
            }
        }
    }
}


- (UIView*)ep_hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
    if (IOS11) {
        CGPoint tempPoint = point;
        if ((point.y < 45 && point.y > 0) &&
            (point.x < ep_defaultFixSpace || point.x > (self.width - ep_defaultFixSpace)))
        {
            if (point.x > ep_defaultFixSpace) {
                tempPoint = CGPointMake(point.x - ep_defaultFixSpace, point.y);
            }
            else{
                tempPoint = CGPointMake(point.x + ep_defaultFixSpace, point.y);
            }
        }
        return [self ep_hitTest:tempPoint withEvent:event];
    }
    else{
        return [self ep_hitTest:point withEvent:event];
    }
}

@end

@implementation UINavigationItem (FixSpace)

+(void)load {
    
    
    if (IOS11) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [self swizzleMethod:@selector(setLeftBarButtonItem:)
                         withMethod:@selector(ep_setLeftBarButtonItem:) error:nil];
            
            [self swizzleMethod:@selector(setLeftBarButtonItems:)
                         withMethod:@selector(ep_setLeftBarButtonItems:) error:nil];
            
            [self swizzleMethod:@selector(setRightBarButtonItem:)
                         withMethod:@selector(ep_setRightBarButtonItem:) error:nil];
            
            [self swizzleMethod:@selector(setRightBarButtonItems:)
                         withMethod:@selector(ep_setRightBarButtonItems:) error:nil];
        });
    }
}

- (void)setEp_isUnFixBarButtonItem:(BOOL)ep_isUnFixBarButtonItem
{
    objc_setAssociatedObject(self, @selector(ep_isUnFixBarButtonItem), [NSNumber numberWithBool:ep_isUnFixBarButtonItem], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


-(BOOL)ep_isUnFixBarButtonItem
{
    NSNumber *ep_isUnFixBarButtonItemNumber = objc_getAssociatedObject(self, @selector(ep_isUnFixBarButtonItem));
    return [ep_isUnFixBarButtonItemNumber integerValue];
}



-(void)ep_setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem {
    if (IOS11 && !ep_disableFixSpace && !self.ep_isUnFixBarButtonItem) {
        [self ep_setLeftBarButtonItem:leftBarButtonItem];
        if ([leftBarButtonItem.customView isKindOfClass:[UIControl class]]) {
            UIControl *leftControl = (UIControl*)leftBarButtonItem.customView;
            leftControl.bounds = CGRectMake(0, 0, kUINavbarButtonWidth, kUINavbarButtonHeight);
            leftControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            if ([leftControl isKindOfClass:[UIButton class]]) {
                UIButton *leftButton = (UIButton *)leftControl;
                [leftButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
                [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
                //                leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, ep_defaultFixSpace, 0, 0);
            }
        }
    }
    else {
        if (!ep_disableFixSpace && leftBarButtonItem) {//存在按钮且需要调节
            [self setLeftBarButtonItems:@[leftBarButtonItem]];
        } else {//不存在按钮,或者不需要调节
            [self ep_setLeftBarButtonItem:leftBarButtonItem];
        }
    }
}

-(void)ep_setLeftBarButtonItems:(NSArray<UIBarButtonItem *> *)leftBarButtonItems {
    if (leftBarButtonItems.count) {
        NSMutableArray *items = [NSMutableArray arrayWithObject:[self fixedSpaceWithWidth:ep_defaultFixSpace-20]];//可修正iOS11之前的偏移
        [items addObjectsFromArray:leftBarButtonItems];
        [self ep_setLeftBarButtonItems:items];
    }
    else {
        [self ep_setLeftBarButtonItems:leftBarButtonItems];
    }
}

-(void)ep_setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem{
    if (IOS11 && !ep_disableFixSpace && !self.ep_isUnFixBarButtonItem) {
        [self ep_setRightBarButtonItem:rightBarButtonItem];
        if ([rightBarButtonItem.customView isKindOfClass:[UIControl class]]) {
            UIControl *rightControl = (UIControl*)rightBarButtonItem.customView;
            rightControl.bounds = CGRectMake(0, 0, kUINavbarButtonWidth, kUINavbarButtonHeight);
            rightControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            if ([rightControl isKindOfClass:[UIButton class]]) {
                UIButton *rightButton = (UIButton *)rightControl;
                [rightButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
                [rightButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
                //                rightButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, ep_defaultFixSpace);
            }
            
        }
        else if (!rightBarButtonItem.customView){
            rightBarButtonItem.width += 16;
        }
    }
    else {
        if (!ep_disableFixSpace && rightBarButtonItem) {//存在按钮且需要调节
            [self setRightBarButtonItems:@[rightBarButtonItem]];
        } else {//不存在按钮,或者不需要调节
            [self ep_setRightBarButtonItem:rightBarButtonItem];
        }
    }
}

-(void)ep_setRightBarButtonItems:(NSArray<UIBarButtonItem *> *)rightBarButtonItems{
    if (rightBarButtonItems.count) {
        NSMutableArray *items = [NSMutableArray arrayWithObject:[self fixedSpaceWithWidth:ep_defaultFixSpace-20]];//可修正iOS11之前的偏移
        [items addObjectsFromArray:rightBarButtonItems];
        [self ep_setRightBarButtonItems:items];
    }
    else {
        [self ep_setRightBarButtonItems:rightBarButtonItems];
    }
}

-(UIBarButtonItem *)fixedSpaceWithWidth:(CGFloat)width {
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                               target:nil
                                                                               action:nil];
    fixedSpace.width = width;
    return fixedSpace;
}

@end
#endif
